<?php

class FormatsController extends AppController {
    
    public $name = 'Formats';
    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');

    public function index() {
        $this->Format->recursive = 0;
        $formats = $this->Format->findAllByStatus("1");
        $this->set('formats', $formats);
    }

    public function view($slug = null) {
        if (!$slug) {
            throw new NotFoundException(__("Invalid id for Format"));
        }
        $format = $this->Format->findBySlug($slug);
        if (!empty($format)) {
            $this->set("format", $format);
        } else {
            throw new NotFoundException(__("Invalid id for Format"));
        }
    }

    public function admin_index() {
        $this->Format->recursive = 0;
        $formats = $this->Format->findAllByStatus("1");
        $this->set('formats', $formats);
    }

    public function admin_add() {
        if ($this->request->is('post')) {
            if (!empty($this->request->data)) {
                $this->Format->create();
                $this->request->data['Format']['slug'] = $this->slug($this->request->data['Format']['name']);
                if ($this->Format->save($this->request->data)) {
                    $this->Session->setFlash("The Format has been saved");
                    $this->redirect(array("action" => "index"));
                } else {
                    $this->Session->setFlash("The Format could not be saved. Please, try again");
                }
            } else {
                $this->Session->setFlash("Data can't be empty. Please, try again");
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$id && empty($this->request->data)) {
            $this->Session->setFlash("Invalid format");
            $this->redirect(array("action" => "index"));
        }
        
        if ($this->request->is('post') || $this->request->is('put')) {
            if (!empty($this->request->data)) {
                $this->request->data['Format']['slug'] = $this->slug($this->request->data['Format']['name']);
                
                if ($this->Format->save($this->request->data)) {
                    $this->Session->setFlash("The Format has been saved");
                    $this->redirect(array("action" => "index"));
                } else {
                    $this->Session->setFlash("The Format could not be saved. Please, try again");
                }
            }
        }
        
        if (empty($this->request->data)) {
            $this->request->data = $this->Format->findById($id);
        }
    }

    public function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash("Invalid if for Format");
            $this->redirect(array("action" => "index"));
        }
        
        $this->Format->id = $id;
        
        if ($this->Format->saveField("status", 0)) {
            $this->Session->setFlash("The Format was successfully deleted");
        } else {
            $this->Session->setFlash("The Format could not be deleted. Please try again");
        }
        $this->redirect(array("action" => "index"));
    }
    
    public function beforeFilter() {
        parent::beforeFilter();
        if (isset($this->request->params["admin"]) && $this->request->params["admin"]) {
            $this->check_user();
        }
    }
}

?>